package com.ruoyi.project.mobile;

import com.ruoyi.common.utils.JWTUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.shiro.service.PasswordService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.info.appointment.domain.Appointment;
import com.ruoyi.project.info.appointment.domain.AppointmentUpdate;
import com.ruoyi.project.info.appointment.service.IAppointmentService;
import com.ruoyi.project.info.doctor.domain.Doctor;
import com.ruoyi.project.info.doctor.service.IDoctorService;
import com.ruoyi.project.info.patient.service.IPatientService;
import com.ruoyi.project.system.user.domain.User;
import com.ruoyi.project.system.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.print.Doc;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/mobile/api")
public class MobileApiController extends BaseController{
    @Autowired
    private IAppointmentService appointmentService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IDoctorService doctorService;
    @Autowired
    private PasswordService passwordService;
    @PostMapping("/getAppointmentList")
    @ResponseBody
    public AjaxResult list(Appointment appointment)
    {
        startPage();
        List<Appointment> list = appointmentService.selectAppointmentList(appointment);
        TableDataInfo info=getDataTable(list);
        return AjaxResult.success("ok-"+info.getTotal(),list);
    }
    @PostMapping("/getAppointmentCount")
    @ResponseBody
    public AjaxResult getAppointmentCount(Appointment appointment)
    {
        appointment.setState("0");
        List<Appointment> list = appointmentService.selectAppointmentList(appointment);
        return AjaxResult.success(list.size());
    }
    @Log(title = "预约信息", businessType = BusinessType.INSERT)
    @PostMapping("/addAppointment")
    @ResponseBody
    public AjaxResult addAppointment(Appointment appointment)
    {
        appointment.setState("0");
        appointment.preInsert();
        return toAjax(appointmentService.insertAppointment(appointment));
    }
    @Log(title = "预约信息", businessType = BusinessType.INSERT)
    @PostMapping("/updateAppointment")
    @ResponseBody
    public AjaxResult updateAppointment(AppointmentUpdate a)
    {
        a.setState("1");
        a.setUpdateTime(new Date());
        return toAjax(appointmentService.updateAppointment(a));
    }
    @PostMapping("/getDoctor")
    @ResponseBody
    public AjaxResult getDoctor(Doctor doctor)
    {
        List<Doctor> list = doctorService.selectDoctorList(doctor);
        return AjaxResult.success("ok",list.get(0));
    }
    @Log(title = "医生管理", businessType = BusinessType.UPDATE)
    @PostMapping("/updateDoctor")
    @ResponseBody
    public AjaxResult updateDoctor(Doctor doctor)
    {
        doctor.preUpdate();
        if(StringUtils.isNotEmpty(doctor.getIdcard())){
            Doctor doctor1=new Doctor();
            doctor1.setIdcard(doctor.getIdcard());
            List<Doctor> doctors=doctorService.selectDoctorList(doctor1);
            if(doctors!=null&&doctors.size()>0){
                boolean isHas=false;
                for(Doctor d:doctors){
                    if(d.getId()!=doctor.getId()){
                        isHas=true;
                        break;
                    }
                }
                if(isHas){
                    return AjaxResult.error("身份证号已存在！");
                }
            }
        }
        return toAjax(doctorService.updateDoctor(doctor));
    }
    @Log(title = "手机端注册", businessType = BusinessType.INSERT)
    @PostMapping("/register")
    @ResponseBody
    public AjaxResult register(Doctor doctor)
    {
        doctor.preInsert();
        Doctor doctor1=new Doctor();
        doctor1.setPhone(doctor.getPhone());
        List<Doctor> doctors=doctorService.selectDoctorList(doctor1);
        if(doctors!=null&&doctors.size()>0){
            return AjaxResult.error("phone");
        }
        if(StringUtils.isNotEmpty(doctor.getIdcard())){
            doctor1=new Doctor();
            doctor1.setIdcard(doctor.getIdcard());
            doctors=doctorService.selectDoctorList(doctor1);
            if(doctors!=null&&doctors.size()>0){
                return AjaxResult.error("idCard");
            }
        }
        return toAjax(doctorService.insertDoctor(doctor));
    }
    @Log(title = "重置密码", businessType = BusinessType.UPDATE)
    @PostMapping("/resetPwd")
    @ResponseBody
    public AjaxResult resetPwd(User user)
    {
        if (userService.resetUserPwd(user) > 0)
        {
            String jwtToken= JWTUtils.token(user.getLoginName(),user.getPassword());
            return success(jwtToken);
        }
        return error();
    }
    @PostMapping("/appLogin")
    @ResponseBody
    public AjaxResult appLogin(User user)
    {
        User u=userService.selectUserByLoginName(user.getLoginName());
        if(u==null){
            String msg = "用户不存在";
            return error(msg);
        }else {
            String p= passwordService.encryptPassword(user.getLoginName(), user.getPassword(), u.getSalt());
            if (!p.equals( u.getPassword().toLowerCase()))
            {
                String msg = "用户或密码错误";
                return error(msg);
            }else {
                if(u.getType().equals("1")){
                    Doctor doctor=new Doctor();
                    doctor.setStatus("0");
                    doctor.setPhone(user.getLoginName());
                    List<Doctor> list = doctorService.selectDoctorList(doctor);
                    if(list.size()==0){
                        String msg = "您的账户已被禁用，请联系管理员！";
                        return error(msg);
                    }
                }
                String jwtToken= JWTUtils.token(u.getLoginName(),u.getPassword());
                return success(jwtToken, u);
            }
        }
    }
}
